
#include"compile_run.hpp"
#include"../comm/httplib.h"

 using namespace ns_compile_and_run;
 using namespace httplib;

int main(int argc,char* argv[])
{   
    if(argc!=2)
    {
        std::cout<<"Usge: "<<argv[0]<<"port"<<std::endl;
    }
    //in_json:{"code":"#inclde";"input":"";"cpu_limit","mem_limit"}
    //out_json{"status","season","stdout","stderr"};
   
    Server ser;
    ser.Get("/hello",[](const Request qst, Response &resp){
       resp.set_content("hello httplib,你好，httplib","text/plain;charset=utf-8");
    }
    );

    ser.Post("/compile_and_run",[](const Request& req,Response& resp){
        std::string in_json=req.body;
        std::string out_json;
        std::cout<<in_json<<std::endl;
        if(!in_json.empty())
        {
            //std::cout<<"1111111"<<std::endl;
            CompileAndRun::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
        }
    });
    ser.listen("0.0.0.0",atoi(argv[1]));//启动http服务

    // Json::Value in_value;
    // in_value["code"]=R"(#include<iostream> 
    // int main()
    // {
    //     std::cout<<"你好,编译服务!"<<std::endl;
    //     return 0;
    //     }
    //     )";
    // in_value["input"]="";
    // in_value["cpu_limit"]=1;
    // in_value["mem_limit"]=10240*30;//相当于1024*30KB
    // Json::FastWriter writer;
    // std::string in_json=writer.write(in_value);
    // std::string out_json;
    // CompileAndRun::Start(in_json,&out_json);
    // std::cout<<out_json<<std::endl;
    return 0;
}